﻿
//表单处理方法
(function ($, ibpm) {
    "use strict";

    /*获取和设置表单数据*/
    $.fn.ibpm_GetFormData = function (keyValue) {// 获取表单数据
        var resdata = {};
        $(this).find('input,select,textarea,.M-select,.M-formselect,.M-Uploader-wrap,.M-Pic-wrap,.M-radio,.M-checkbox,.edui-default').each(function (r) {
            var id = $(this).attr('id');
            if (!!id) {
                var type = $(this).attr('type');
                switch (type) {
                    case "radio":
                        if ($("#" + id).is(":checked")) {
                            var _name = $("#" + id).attr('name');
                            resdata[_name] = $("#" + id).val();
                        }
                        break;
                    case "checkbox":
                        if ($("#" + id).is(":checked")) {
                            resdata[id] = 1;
                        } else {
                            resdata[id] = 0;
                        }
                        break;
                    case "Mselect":
                        resdata[id] = $(this).ibpm_selectGet();
                        break;
                    case "GirdSelect":
                        resdata[id] = $(this).ibpm_GirdSelectGet();
                        break;
                    case "upload":
                        resdata[id] = $(this).ibpm_UploaderGet();
                        break;
                    case "pic":
                        resdata[id] = $(this).ibpm_picGet();
                        break;
                    case "radio":
                        resdata[id] = $(this).find('input:checked').val();
                        break;
                    case "checkbox":
                        var _idlist = [];
                        $(this).find('input:checked').each(function () {
                            _idlist.push($(this).val());
                        });
                        resdata[id] = String(_idlist);
                        break;
                    default:
                        if ($("#" + id).hasClass('currentInfo')) {
                            var value = $("#" + id)[0].Mvalue;
                            resdata[id] = $.trim(value);
                        }
                        else if ($(this).hasClass('edui-default')) {
                            if ($(this)[0].ue) {
                                resdata[id] = $(this)[0].ue.getContent();
                            }
                        }
                        else {
                            var value = $("#" + id).val();
                            resdata[id] = $.trim(value);
                        }

                        break;
                }
                resdata[id] += '';
                if (resdata[id] == '') {
                    resdata[id] = '&nbsp;';
                }
                if (resdata[id] == '&nbsp;' && !keyValue) {
                    resdata[id] = '';
                }
            }
        });
        return resdata;
    };
    $.fn.ibpm_SetFormData = function (data) {// 设置表单数据
        var $this = $(this);
        for (var id in data) {
            var $obj = $this.find('#' + id);
            var value = $.trim(data[id]).replace(/\//g, '');
            
            if ($obj.length == 0 && value != null) {
                $obj = $this.find('[name="' + id + '"][value="' + value + '"]');
                if ($obj.length > 0) {
                    if (!$obj.is(":checked")) {
                        $obj.trigger('click');
                    }
                }
            }
            else {
                var type = $obj.attr('type');
                if ($obj[0] == undefined || $obj[0] == null) {
                    var Ban = $this.find("input[name='" + id + "'][type='radio']");
                    if (Ban[0] != undefined && Ban[0] != null) {
                        type = "radio";
                    }
                }
                else if ($obj[0].tagName.toUpperCase() == 'SELECT') {
                    type = "select";
                }
                else if ($obj.hasClass("M-input-wdatepicker")) {
                    type = "datepicker";
                }
                switch (type) {
                    case "checkbox":
                        if (value == "true") {
                            $obj.attr("checked", 'checked');
                            $obj.prop("checked", true);
                        } else {
                            $obj.prop("checked", false);
                            $obj.removeAttr("checked");
                        }
                        break;
                    case "select":
                        $obj.val(value).trigger("change");
                        break;
                    case "radio":
                        var v0 = $("#" + id + '0').val();
                        var v1 = $("#" + id + '1').val();
                        $("#" + id + '0').removeAttr("checked");
                        $("#" + id + '1').removeAttr("checked");
                        if (v0 == value) {
                            $("#" + id + '0').attr("checked", true);
                        }
                        else {
                            $("#" + id + '1').attr("checked", true);
                        }
                        break;
                    case "Mselect":
                        $obj.ibpm_selectSet(value);
                        break;
                    case "GirdSelect":
                        $obj.ibpm_GirdSelectSet(value);
                        break;
                    case "datepicker":
                        $obj.val(ibpm.formatDate(value, 'yyyy-MM-dd'));
                        break;
                    case "upload":
                        $obj.ibpm_UploaderSet(value);
                        break;
                    case "pic":
                        $obj.ibpm_picSet(value);
                        break;
                    case "M-radio":
                        if (!$obj.find('input[value="' + value + '"]').is(":checked")) {
                            $obj.find('input[value="' + value + '"]').trigger('click');
                        }
                        break;
                    default:
                        if ($obj.hasClass('currentInfo')) {
                            $obj[0].Mvalue = value;
                            $obj.val(value);
                        }
                        else if ($obj.hasClass('edui-default')) {
                            setUe($obj[0].ue, value);
                        }
                        else {
                            $obj.val(value);
                        }
                        break;
                }
            }
        }
    };

    function setUe(ue, value) {
        ue.ready(function () {
            ue.setContent(value);
        });
    }

    /*表单数据操作*/
    $.BPM_SetForm = function (url, callback) {
        ibpm.loading(true, '正在获取数据');
        ibpm.httpAsyncGet(url, function (res) {
            if (res.code == ibpm.httpCode.success) {
                callback(res.data);
            }
            else {
                ibpm.layerClose(window.name);
                ibpm.alert.error('表单数据获取失败,请重新获取！');
                ibpm.httpErrorLog(res.info);
            }
        });
    };

    /*固定下拉框的一些封装：数据字典*/

    // 数据字典下拉框
    $.fn.ibpm_DataItemSelect = function (op) {
        var dfop = {
            code: false,
            // 是否允许搜索
            allowSearch: false,
            // 访问数据接口参数
            param: { itemCode: '', parentId: '0' },
        }
        op = op || {};
        if (!op.code) {
            return $(this);
        }

        dfop.param.itemCode = op.code;
        dfop.param.parentId = op.parentId || '0';
        dfop.allowSearch = op.allowSearch;

        var list = [];

        var $select = $(this).ibpm_select(dfop);
        ibpm.clientdata.getAllAsync('dataItem', {
            code: dfop.param.itemCode,
            callback: function (dataes) {
                $.each(dataes, function (_index, _item) {
                    list.push({ id: _item.value, text: _item.text, title: _item.text, k: _index });
                });
                $select.ibpm_selectRefresh({
                    data: list
                });
            }
        });
        return $select;
    };    

})(jQuery, top.ibpm);
